package com.ji.server;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ji.bean.DataBean;
import com.ji.bean.ParamBean;
import com.ji.dao.JdbcUtil;
import com.ji.util.Guid;
import com.ji.vo.ResultVo;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.*;
import com.rabbitmq.client.QueueingConsumer.Delivery;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class CloudServer {

    public static final String RPC_QUEUE_NAME = "rpc_cloud";

    public static String sayHello(String name){
        return "hello " + name ;
    }

    public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException,TimeoutException {

        ConnectionFactory connFac = new ConnectionFactory() ;
        connFac.setHost("192.168.1.106");
        connFac.setPort(5672);
        connFac.setUsername("root");
        connFac.setPassword("root");
        Connection conn = connFac.newConnection() ;

        Channel channel = conn.createChannel() ;

        channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null) ;

        QueueingConsumer consumer = new QueueingConsumer(channel);

        channel.basicConsume(RPC_QUEUE_NAME, false , consumer) ;

        while(true){
            System.out.println("服务端等待接收消息..");
            Delivery deliver = consumer.nextDelivery() ;
            System.out.println("服务端成功收到消息..");
            BasicProperties props =  deliver.getProperties() ;

            String resultStr = new String(deliver.getBody() , "UTF-8") ;
            System.out.println("接受的消息"+resultStr);





            Gson gson = new GsonBuilder().create();
            ResultVo vo = new ResultVo();
            int insertCount = 0;
            try {


                // 接收并打印客户端的信息
                //LOG.info("客户端请求的参数是:".concat(resultStr));
                int t = resultStr.indexOf("type") + 7;
                String typeValue = resultStr.substring(t, t + 1);
                switch (typeValue) {
                    case "1":// 数据上传
                        DataBean data = gson.fromJson(resultStr, DataBean.class);
                        DataBean dBean = new DataBean();
                        dBean.setId(Guid.getUuid());
                        dBean.setDeviceId(data.getDeviceId());
                        dBean.setTemperature(data.getTemperature());
                        dBean.setAlarmMask(data.getAlarmMask());
                        int p1 = resultStr.indexOf("probeTempList") + 17;
                        int p2 = resultStr.indexOf("setValueList") - 4;
                        String probList = resultStr.substring(p1, p2).replace("\"", "");
                        int v1 = resultStr.indexOf("setValueList") + 16;
                        int v2 = resultStr.indexOf("statusMask") - 4;
                        String valueList = resultStr.substring(v1, v2).replace("\"", "");
                        dBean.setProbeTemp(probList);
                        dBean.setSetValue(valueList);
                        dBean.setInputMask(data.getInputMask());
                        dBean.setStatusMask(data.getStatusMask());
                        dBean.setOutputMask(data.getOutputMask());
                        dBean.setSetup(data.getSetup());
                        insertCount = JdbcUtil.insert(dBean);
                        //LOG.debug("插入" + insertCount + "条");
                        System.out.println("插入" + insertCount + "条");
                        vo.setCode("1");
                        break;
                    case "2": // 参数上传
                        ParamBean Dparam = gson.fromJson(resultStr, ParamBean.class);
                        ParamBean param = new ParamBean();
                        param.setId(Guid.getUuid());
                        param.setDeviceId(Dparam.getDeviceId());
                        p1 = resultStr.indexOf("paramAddressList") + 20;
                        p2 = resultStr.indexOf("paramValueList") - 4;
                        probList = resultStr.substring(p1, p2).replace("\"", "");
                        v1 = resultStr.indexOf("paramValueList") + 18;
                        v2 = resultStr.indexOf("}") - 2;
                        valueList = resultStr.substring(v1, v2).replace("\"", "");
                        param.setParamAddress(probList);
                        param.setParamValue(valueList);
                        insertCount = JdbcUtil.insert(param);
                        //LOG.debug("插入" + insertCount + "条");
                        System.out.println("插入" + insertCount + "条");
                        vo.setCode("1");
                        break;
                    case "3": // 参数修改
                        ParamBean Dparam1 = gson.fromJson(resultStr, ParamBean.class);
                        ParamBean param1 = new ParamBean();
                        param1.setDeviceId(Dparam1.getDeviceId());
                        p1 = resultStr.indexOf("paramAddressList") + 20;
                        p2 = resultStr.indexOf("paramValueList") - 4;
                        probList = resultStr.substring(p1, p2).replace("\"", "");
                        v1 = resultStr.indexOf("paramValueList") + 18;
                        v2 = resultStr.indexOf("}") - 2;
                        valueList = resultStr.substring(v1, v2).replace("\"", "");
                        param1.setParamAddress(probList);
                        param1.setParamValue(valueList);
                        insertCount = JdbcUtil.update(param1);
                        //LOG.debug("修改" + insertCount + "条");
                        System.out.println("插入" + insertCount + "条");
                        vo.setObj(param1);
                        break;
                    case "4": // 接收参数修改返回数据
                        ParamBean Dparam2 = gson.fromJson(resultStr, ParamBean.class);
                        ParamBean param2 = new ParamBean();
                        param2.setDeviceId(Dparam2.getDeviceId());
                        p1 = resultStr.indexOf("paramAddressList") + 20;
                        p2 = resultStr.indexOf("paramValueList") - 4;
                        probList = resultStr.substring(p1, p2).replace("\"", "");
                        v1 = resultStr.indexOf("paramValueList") + 18;
                        v2 = resultStr.indexOf("}") - 2;
                        valueList = resultStr.substring(v1, v2).replace("\"", "");
                        param2.setParamAddress(probList);
                        param2.setParamValue(valueList);
                        insertCount = JdbcUtil.update(param2);
                        vo.setObj(param2);
                        break;
                    default:
                        vo.setCode("0");
                        break;
                }

            } catch (Exception e) {
                //LOG.error(e.getMessage());
                vo.setCode("0");
            }



            String responseMessage = sayHello(resultStr) ;

            BasicProperties responseProps = new BasicProperties.Builder()
                    .correlationId(props.getCorrelationId())
                    .build() ;

            //将结果返回到客户端Queue
            channel.basicPublish("", props.getReplyTo() , responseProps , responseMessage.getBytes("UTF-8") ) ;

            //向客户端确认消息
            channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
            System.out.println("服务端返回消息完成..");
        }

    }

}

